import cv2 as cv
import matplotlib
import numpy as np

matplotlib.use('TkAgg')
from matplotlib import pyplot as plt


def main():
    img = cv.imread('../data/sudoku.png')

    plt.figure(figsize=(15, 15))

    pts1 = np.float32([[73, 87], [490, 71], [36, 515], [520, 520]])
    pts2 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]])
    M1 = cv.getPerspectiveTransform(pts1, pts2)
    print("第一次透视变换矩阵：")
    print(M1)
    dst1 = cv.warpPerspective(img, M1, (300, 300))
    plt.subplot(121), plt.imshow(img), plt.title('Input')
    plt.subplot(122), plt.imshow(dst1), plt.title('Output')
    plt.show()

    pts3 = np.float32([[354, 360], [510, 356], [356, 515], [520, 520]])
    M2 = cv.getPerspectiveTransform(pts3, pts2)
    print("第二次透视变换矩阵：")
    print(M2)
    dst2 = cv.warpPerspective(img, M2, (300, 300))
    plt.figure(figsize=(15, 15))
    plt.subplot(121), plt.imshow(img), plt.title('Input')
    plt.subplot(122), plt.imshow(dst2), plt.title('Output')
    plt.show()


if __name__ == "__main__":
    main()
